package com.ym.b2b.order.service.impl;

import java.util.Date;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ym.b2b.commons.exception.BaseException;
import com.ym.b2b.commons.service.BaseService;
import com.ym.b2b.commons.util.CheckData;
import com.ym.b2b.order.dao.SalesDetailDao;
import com.ym.b2b.order.service.SalesDetailService;
import com.ym.b2b.order.sqlbean.EmSSaleDetail;
import com.ym.b2b.user.dao.EmCUserDAO;
import com.ym.b2b.user.sqlbean.EmCUser;
import com.zc.commons.base.Page;
import com.zc.commons.base.Result;

@Service
public class SalesDetailServiceImpl extends BaseService implements SalesDetailService {

    private Logger         logger = Logger.getLogger(getClass());

    @Autowired
    private SalesDetailDao salesDetailDao;

    @Autowired
    private EmCUserDAO     userDao;

    @Override
    public Page<EmSSaleDetail> searchPageSalesDetail(EmSSaleDetail emSSaleDetail, Page<EmSSaleDetail> page)
                                                                                                           throws BaseException {

        return salesDetailDao.searchPageSalesDetail(emSSaleDetail, page);
    }

    @Override
    public Result salesDetailValidQuery(EmSSaleDetail emSSaleDetail) throws BaseException {
        Result result = new Result();
        // 验证 是否已经过期
        EmCUser emCUser = new EmCUser();
        emCUser.setLoginName(emSSaleDetail.getLoginName());
        EmCUser newemCUser = userDao.searchEmCUser(emCUser);

        if (newemCUser.getQueryStart() == null || newemCUser.getQueryEnd() == null) {
            result.setCode(Result.CODE_100);
            result.setErrorMsg("账户 时间设置 为空  请联系网络部!");
            return result;
        }

        Date now = new Date();
        if (now.after(newemCUser.getQueryEnd())) {
            result.setCode(Result.CODE_100);
            result.setErrorMsg("您的查询截止日期为：" + CheckData.returnDate(newemCUser.getQueryEnd()) + " 请联系网络部.");
            return result;
        }
        emSSaleDetail.setQueryStart(newemCUser.getQueryStart());
        emSSaleDetail.setQueryEnd(newemCUser.getQueryEnd());
        // result.putData("queryStart", newemCUser.getQueryStart());
        return result;
    }

    /**
      * <p>Title: searchPageSalesDetailList</p>
      * Description:前台供应商查销售流向
      * @param emSSaleDetail
      * @param page
      */
    @Override
    public Page<EmSSaleDetail> searchPageSalesDetailList(EmSSaleDetail emSSaleDetail, Page<EmSSaleDetail> page)
                                                                                                               throws BaseException {
        try {
            return salesDetailDao.searchPageSalesDetail(emSSaleDetail, page);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BaseException("销售流向查询异常");
        }
    }
}
